from typing import Union, Optional

from configs import config

from models import execute


async def get_spu_info(id) -> list:
    """查出good_spu的所有字段信息"""
    _, spu = await execute('select id, cid, title, price, detail, img from good where id = %s', (id,))
    return spu

async def query_spu_is_collect(id) -> bool:
    spu_ids = await config.redis.lrange('collect_%s' % 'user_id', 0, -1)
    is_collect = False
    if id in spu_ids:
        is_collect = True
    return is_collect

async def get_sku_from_spu(id) -> list:
    _, sku_id_list = await execute('select id from good_sku where good_id = %s', (id,))
    return sku_id_list

async def get_property_from_sku(new_sku_id_list) -> list:
    _, property_val_list = await execute \
        ('select property_id, title from property_value where sku_id in (%s)' % ','.join(['%s'] *len(new_sku_id_list)), tuple(new_sku_id_list))
    return property_val_list

async def get_property_title_form_id(property_id_list) -> list:
    _, property_list = await execute \
        ('select id, title from property where id in (%s)' % ','.join(['%s'] *len(property_id_list)), tuple(property_id_list))
    return property_list